import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemPostApi } from '#/api/system/post';

import { getDeptList } from '#/api/system/dept';
import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'postName',
      label: $t('system.post.postName'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'postCode',
      label: $t('system.post.postCode'),
      rules: 'required',
    },
    {
      fieldName: 'deptId',
      label: $t('system.user.deptId'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptList();
          return data;
        },
        class: 'w-full',
        labelField: 'deptName',
        valueField: 'deptId',
        childrenField: 'children',
        placeholder: '请选择归属部门',
        treeDefaultExpandAll: true,
      },
      rules: 'required',
    },
    {
      component: 'InputNumber',
      fieldName: 'showSort',
      label: $t('system.post.showSort'),
      defaultValue: 0,
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'postStatus',
      label: $t('system.post.postStatus'),
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('system.post.remark'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'postName',
      label: $t('system.post.postName'),
    },
    {
      component: 'Input',
      fieldName: 'postCode',
      label: $t('system.post.postCode'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
      },
      fieldName: 'postStatus',
      label: $t('system.post.postStatus'),
    },
    {
      component: 'Input',
      fieldName: 'remark',
      label: $t('system.post.remark'),
    },
    {
      component: 'RangeDatePicker',
      fieldName: 'createTime',
      label: $t('system.post.createTime'),
    },
  ];
}

export function useColumns<T = SystemPostApi.SystemPost>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'postName',
      title: $t('system.post.postName'),
    },
    {
      field: 'postCode',
      title: $t('system.post.postCode'),
    },
    {
      cellRender: { name: 'CellTag' },
      field: 'postStatus',
      title: $t('system.post.postStatus'),
    },
    {
      field: 'deptName',
      title: $t('system.post.deptName'),
    },
    {
      field: 'showSort',
      title: $t('system.post.showSort'),
    },
    {
      field: 'remark',
      title: $t('system.post.remark'),
    },
    {
      field: 'createTime',
      title: $t('system.post.createTime'),
      formatter: 'formatDateTime',
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'postName',
          nameTitle: $t('system.post.postName'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.post.operation'),
      width: 130,
    },
  ];
}
